﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.Properties;
using System.Diagnostics;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
	partial class GenericFolderNodeExtension
	{
        public void InitializeThemeGallery(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += ThemeGallery_NodeChildrenRequested;
            nodeType.NodeMenuItemsRequested += ThemeGallery_NodeMenuItemsRequested;
            nodeType.NodePropertiesRequested += ThemeGallery_NodePropertiesRequested;
        }

        /// <summary>
        /// Get the properties for the theme gallery
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodePropertiesRequestedEventArgs object</param>
        void ThemeGallery_NodePropertiesRequested(object sender, ExplorerNodePropertiesRequestedEventArgs e)
        {
            //Check this is the web part gallery node
            if (e.Node.Text == Resources.ThemesNode_Label)
            {
                var themeGalleryNodeInfo = e.Node.GetAnnotation<ThemeGalleryNodeInfo>();

                // Call the custom SharePoint command to get the Web Part properties.
                Dictionary<string, string> properties =
                    e.Node.Context.SharePointConnection.ExecuteCommand<
                    ThemeGalleryNodeInfo, Dictionary<string, string>>(
                    ThemeGalleryCommandIds.GetThemeGalleryProperties, themeGalleryNodeInfo);

                object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
                e.PropertySources.Add(propertySource);
            }
        }

        /// <summary>
        /// Register the menu items for the theme gallery
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeMenuItemsRequestedEventArgs object</param>
        void ThemeGallery_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            if (e.Node.Text == Resources.ThemeGalleryNode_Label)
            {
                //Register the view in browser menu item
                e.MenuItems.Add(Resources.ThemeGalleryNode_ViewInBrowser).Click += ThemeGalleryGenericFolderNodeExtension_Click;
            }
        }



        void ThemeGallery_NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// Open the theme gallery in a browser
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The MenuItemEventArgs object</param>
        void ThemeGalleryGenericFolderNodeExtension_Click(object sender, MenuItemEventArgs e)
        {
            IExplorerNode owner = (IExplorerNode)e.Owner;

            string allItemsUrl =
            owner.Context.SharePointConnection.ExecuteCommand<string>(ThemeGalleryCommandIds.GetThemeGalleryAllItemsUrl);

            if (allItemsUrl.StartsWith(@"/"))
            {
                allItemsUrl = allItemsUrl.TrimStart(@"/".ToCharArray());
            }
            Process.Start(new Uri(owner.Context.SiteUrl + allItemsUrl).AbsoluteUri);

        }
    }
}

